package javatwo.annotation.course2.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;


import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet(name = "uploadServlet", urlPatterns = "/upload")
@MultipartConfig(location = "c:\\tmp", fileSizeThreshold = 1024 * 1024, maxFileSize = 1024 * 1024 * 5, maxRequestSize = 1024 * 1024 * 5 * 5)
public class FileUploadServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		getServletContext().getRequestDispatcher("/form.jsp")
				.forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();

		Collection<Part> parts = req.getParts();

		out.write("<h2> Total parts : " + parts.size() + "</h2>");

		for (Part part : parts) {
			printPart(part, out);
			String cdHeader = part.getHeader("content-disposition");
			System.out.println(cdHeader);
			part.write(getFileName(cdHeader));
		}

	}

	private void printPart(Part part, PrintWriter pw) {
		StringBuffer sb = new StringBuffer();
		sb.append("<p>");
		sb.append("Name : " + part.getName());
		sb.append("<br>");
		sb.append("Content Type : " + part.getContentType());
		sb.append("<br>");
		sb.append("Size : " + part.getSize());
		sb.append("<br>");
		for (String header : part.getHeaderNames()) {
			sb.append(header + " : " + part.getHeader(header));
			sb.append("<br>");
		}
		sb.append("</p>");
		pw.write(sb.toString());
	}

	private String getFileName(String neededHeader) {
		String fileName = null;
		for (String onePiece : neededHeader.split(";")) {
			if (onePiece.contains("filename=")) {
				String myPieces[] = onePiece.split("=");
				fileName = myPieces[1].replaceAll("\"", "").trim();
			}
		}

		if (null == fileName) {
			throw new IllegalArgumentException("The Header provided seems to be Invalid!");
		}
		return fileName;
	}
}